package com.example.easeimpro.model.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.example.easeimpro.model.bean.UserInfo;
import com.example.easeimpro.model.db.UserAccountDB;

/**
 * 用户数据库的操作类
 */
public class UserAccountDao {
    private static final String TAG = "UserAccountDao";
    private static SQLiteDatabase db;
    public UserAccountDao(Context context) {
       UserAccountDB help = new UserAccountDB(context);
        db = help.getWritableDatabase();
    }


    public  void addAccount(UserInfo user){
        ContentValues values = new ContentValues();
        values.put(UserAccountTable.COL_NAME,user.getName());
        values.put(UserAccountTable.COL_HXID,user.getHxId());
        values.put(UserAccountTable.COL_NICK,user.getNick());
        values.put(UserAccountTable.COL_PHOTO,user.getPhoto());

        //插入用户
        db.replace(UserAccountTable.TAB_NAME,null,values);
        db.close();
    }

    public  UserInfo getAccountByHxId(String hxId){
        //获取数据库对象

        //执行查询语句
        String sql = "select * from "+UserAccountTable.TAB_NAME +" where "+UserAccountTable.COL_HXID+"=?";
        Cursor cursor = db.rawQuery(sql,new String[]{hxId});
        Log.e(TAG, "getAccountByHxId: "+cursor.getCount() );
        UserInfo userInfo = null;
        if (cursor.moveToNext()){
            userInfo = new UserInfo();

            userInfo.setHxid(cursor.getString(0));
            userInfo.setName(cursor.getString(1));
            userInfo.setNick(cursor.getString(2));
            userInfo.setPhoto(cursor.getString(3));


        }

        //关闭资源
        db.close();
        //返回数据
        return userInfo;
    }
}
